<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Untitled Page" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>

<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="ClubStarterKit.Data" %>
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Request("articleid") Is Nothing = False Then
            Dim news As New Announcements(Request("articleid"))
            If news.IsLoaded = True Then
                NewsTitle.Text = news.Title.ToString()
                NewsTitle2.Text = news.Title.ToString()
            Else
                Response.Redirect("~/news/list.aspx")
            End If
            
            If Request("delref") Is Nothing = False Then
                DeleteRef(Request("delref"))
            End If
        Else
            Response.Redirect("~/news/list.aspx")
        End If
        HyperLink2.NavigateURL = "~/Admin/AddDownload.aspx?ReturnURL=News/Download.aspx?articleid=" & CStr(Request("articleid"))
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClubStarterKit.Data.DownloadsInNews.Insert(CInt(Request("articleid")), Integer.Parse(DownloadDropDown.SelectedValue.ToString()))
        Response.Redirect("~/News/Download.aspx?articleid=" & Request("articleid"))
    End Sub
    
    Public Sub DeleteRef(ByVal reference As Object)
        Dim refid As Integer = Integer.Parse(reference.ToString())
        Dim ref As New DownloadsInNews(refid)
        If ref.IsLoaded = True Then
            ClubStarterKit.Data.DownloadsInNews.Delete(refid)
        End If
        Response.Redirect("~/news/downloads.aspx?articleid=" & CStr(Request("articleid")))
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="body">
    <div class="fullwidth">
        <h2>Downloads for News Article: <asp:Label ID="NewsTitle" runat="server"></asp:Label></h2>
        
        <p><asp:GridView id="GridView1" runat="server" DataSourceID="NewsDownloadsDS" AutoGenerateColumns="False" 
            DataKeyNames="id" AllowPaging="False" AutoGenerateDeleteButton="False" Width="683px" ShowHeader="False" 
            GridLines="None">
                <EmptyDataTemplate>
                    There are no downloads added to this article.
                </EmptyDataTemplate>
                <Columns>
                    <asp:BoundField DataField="Title" SortExpression="Title" />
                    <asp:TemplateField>
                            <ItemTemplate>
                                <asp:HyperLink ID="HyperLink1" Text="Download" runat="server" NavigateUrl='<%# Eval("URL","~/Documents/{0}") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                        SortExpression="id" Visible="False" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Hyperlink ID="Delbutton" Text="Delete" runat="server" NavigateURL='<%# "~/news/downloads.aspx?delref=" & CStr(Eval("ID")) & "&articleid=" & CStr(Request("articleid")) %>' />
                            </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            </p>
            <p>
            </p>
            </div>
            <asp:ObjectDataSource ID="NewsDownloadsDS" runat="server" SelectMethod="ArticleDownloads" TypeName="ClubStarterKit.Web.News">
                <SelectParameters>
                    <asp:QueryStringParameter Name="id" QueryStringField="articleid" Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
    &nbsp;
            <asp:ObjectDataSource ID="DropDownSource" runat="server" SelectMethod="FetchAll" TypeName="ClubStarterKit.Data.Downloads" />
            </div>
<div class="fullwidth">
<h2>Add Download to <asp:Label ID="NewsTitle2" runat="server" /></h2>
<p><asp:DropDownList id="DownloadDropDown" runat="server" DataSourceID="DropDownSource" DataTextField="Title" DataValueField="id">
            </asp:DropDownList>&nbsp;</p><p><Club:RolloverButton id="Button1" onclick="Button1_Click" runat="server" Text="Add"></Club:RolloverButton>&nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <Club:RolloverLink id="HyperLink2" runat="server" Text="Upload">
        </Club:RolloverLink>&nbsp;</p>
</div>
</asp:Content>

